package com.treasure.mall.zbqmallmanager.controller.blacklist;

import com.treasure.mall.biz.dto.blacklist.BlacklistSaveDTO;
import com.treasure.mall.biz.dto.condition.BlacklistSearchCondition;
import com.treasure.mall.biz.entity.blacklist.BlacklistPO;
import com.treasure.mall.biz.service.core.blacklist.BlacklistService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 
 * <p> 黑名单controller
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallmanager.controller.blacklist
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/23 2:51 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Controller
@RequestMapping("/blacklist")
public class BlacklistController {

    @Autowired
    private BlacklistService blacklistService;

    /**
     * 黑名单列表
     *
     * @param condition
     * @param pageIndex
     * @param model
     * @return
     */
    @GetMapping("/list")
    @SuppressWarnings("Duplicates")
    public String list(
            @ModelAttribute("condition") BlacklistSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<BlacklistPO> pos = blacklistService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", pos.getTotalElements());
        model.addAttribute("totalPages", pos.getTotalPages());
        model.addAttribute("list", pos.getContent());
        return "blacklist/list";
    }

    /**
     * 添加黑名单
     *
     * @param dto
     * @return
     */
    @PostMapping("/list/save")
    @ResponseBody
    public ApiResult save(BlacklistSaveDTO dto) {
        blacklistService.save(dto);
        return ApiResult.ok();
    }

    /**
     * 删除黑名单
     *
     * @param id
     * @return
     */
    @GetMapping("/list/delete")
    @ResponseBody
    public ApiResult deleteById(@RequestParam Long id) {
        blacklistService.delete(id);
        return ApiResult.ok();
    }


}
